﻿using System.Diagnostics;
using App_Code.Helpers;
using PluginDB;

namespace App_Code.Rules
{
    /// <summary>
    /// Summary description for VerifyBrokenLinkRule
    /// </summary>
    public class VerifyBrokenLinkRule : IRule
    {
        public void ApplyRule(PluginVideoInfo pluginVideoInfo)
        {
            foreach (var link in pluginVideoInfo.Links)
            {
                var videoResult = ExtractVideoFullLink(link.LinkURL);
                if (!videoResult.IsValid)
                {
                    Debug.WriteLine(string.Format("Link is not valid: Name[{0}] - Link[{1}]", videoResult.MovieName, videoResult.Link));
                    return;
                }

                link.IsBrokenLink = Utils.IsBrokenLink(videoResult.Link);
            }
        }

        private VideoResult ExtractVideoFullLink(string url)
        {
            if (VideoBBExtractor.IsContainVideoLink(url))
            {
                return VideoBBExtractor.ExtractMovieUrl(url);
            }

            if (VideozerExtractor.IsContainVideoLink(url))
            {
                return VideozerExtractor.ExtractMovieUrl(url);
            }

            if (MytubeExtractor.IsContainVideoLink(url))
            {
                return MytubeExtractor.ExtractMovieUrl(url);
            }

            if (SdarotOrgExtractor.IsContainVideoLink(url))
            {
                return SdarotOrgExtractor.ExtractMovieUrl(url);
            }

            if (MultiUploadExtractor.IsContainVideoLink(url))
            {
                return MultiUploadExtractor.ExtractMovieUrl(url);
            }

            if (MegaVideoExtractor.IsContainVideoLink(url))
            {
                return MegaVideoExtractor.ExtractMovieUrl(url);
            }

            if (CuevanaTVExtractor.IsContainVideoLink(url))
            {
                return CuevanaTVExtractor.ExtractMovieUrl(url);
            }

            return null;
        }
    }
}